// 购物车相关接口
import type { CartItem } from '@/types/cart'
import request from '@/utils/request'

enum API {
  // 添加购物车
  AddCart_URL = '/member/cart',
  // 获取购物车列表
  GetCartList_URL = '/member/cart',
  // 删除/清空购物车单品,
  DelCartGoods_URL = '/member/cart',
  // 修改购物车单品
  UpdateCartGoods_URL = '/member/cart/',
  // 购物车全选/取消全选
  SelectedAll_URL = '/member/cart/selected'
}

/**
 * 加入购物车
 * @param {string} skuId skuID
 * @param {number} count 商品数量
 */
export const reqAddCart = (skuId: string, count: number) => {
  return request({
    url: API.AddCart_URL,
    method: 'POST',
    data: {
      skuId,
      count
    }
  })
}

/**
 * 获取购物车列表
 */
export const reqGetCartList = () => {
  return request<CartItem[]>({
    url: API.AddCart_URL,
    method: 'GET'
  })
}

/**
 * 删除/清空购物车单品
 * @param {string[]} ids 删除商品 skuID 集合
 */
export const reqDelCartGoods = (ids: string[]) => {
  return request({
    url: API.DelCartGoods_URL,
    method: 'DELETE',
    data: {
      ids
    }
  })
}

// 修改购物车单品
// PUT/member/cart/{skuId}
/**
 *
 * @param {string} skuId skuId
 * @param {UpdateData} data body 参数
 */
type UpdateData = {
  selected?: boolean
  count?: number
}
export const reqUpdateCartGoods = (skuId: string, data: UpdateData) => {
  return request({
    url: API.UpdateCartGoods_URL + skuId,
    method: 'PUT',
    data
  })
}

/**
 * 购物车全选/取消全选
 * @param {boolean} selected 全选状态
 */
export const reqCartSelectedAll = (selected: boolean) => {
  return request({
    url: API.SelectedAll_URL,
    method: 'PUT',
    data: {
      selected
    }
  })
}
